package com.hengtongxing.hejiayun.mine;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.MyHouseListBean;
import com.hengtongxing.hejiayun.event.HouseEvent;
import com.hengtongxing.hejiayun.mine.adapter.HouseListAdapter;
import com.hengtongxing.hejiayun.mine.model.IMineModel;
import com.hengtongxing.hejiayun.mine.model.MineModelImpl;
import com.hengtongxing.hejiayun.widget.AppTitleBar;
import com.hengtongxing.hejiayun.widget.WrapContentLinearLayoutManager;

import org.greenrobot.eventbus.Subscribe;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的房屋
 */
public class HouseListActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.iv_edit)
    ImageView ivEdit;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_commit)
    LinearLayout llCommit;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_other_house)
    TextView tvOtherHouse;
    @BindView(R.id.tv_curr_house)
    TextView tvCurrHouse;
    @BindView(R.id.ll_curr_house)
    LinearLayout llCurrHouse;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private IMineModel mineModel;
    private HouseListAdapter listAdapter;
    private List<MyHouseListBean.DataBean> beanList;
    private MyHouseListBean.DataBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_list);
        ButterKnife.bind(this);
        registerEventBus();
        initView();
        requestMyHouse();
    }

    public void initView() {
        beanList = new ArrayList<>();
        mineModel = new MineModelImpl();
        listAdapter = new HouseListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener(position -> {
            Intent intent = new Intent(mActivity, AddHouseActivity.class);
            intent.putExtra("dataBean", beanList.get(position));
            startActivity(intent);
        });
    }

    @Subscribe
    public void onUpDate(HouseEvent event) {
        requestMyHouse();
    }

    @OnClick({R.id.iv_edit, R.id.ll_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_edit:
                if (dataBean != null) {
                    Intent intent = new Intent(mActivity, AddHouseActivity.class);
                    intent.putExtra("dataBean", dataBean);
                    startActivity(intent);
                }
                break;
            case R.id.ll_commit:
                showActivity(AddHouseActivity.class);
                break;
        }
    }

    public void requestMyHouse() {
        mineModel.requestMyHouse(new DataCallBack<MyHouseListBean>() {
            @Override
            public void onSuccessful(MyHouseListBean bean) {
                beanList.clear();
                if (bean.getData().size() > 0) {
                    beanList.addAll(bean.getData());
                    tvCurrHouse.setVisibility(View.VISIBLE);
                    llCurrHouse.setVisibility(View.VISIBLE);
                    tvOtherHouse.setVisibility(View.VISIBLE);
                    llNoData.setVisibility(View.GONE);
                }
                if (beanList.size() == 1) {
                    dataBean = beanList.get(0);
                    tvName.setText(dataBean.getComm_name());
                    tvAddress.setText(dataBean.getBuilding() + dataBean.getUnit_name() + dataBean.getFloor() + dataBean.getHose_number());
                    beanList.clear();
                    tvOtherHouse.setVisibility(View.GONE);
                } else if (beanList.size() > 1) {
                    for (MyHouseListBean.DataBean data : beanList) {
                        if (data.getIs_def() == 1) {
                            dataBean = data;
                        }
                    }
                    if (dataBean == null) {
                        dataBean = beanList.get(0);
                    }
                    tvName.setText(dataBean.getComm_name());
                    tvAddress.setText(dataBean.getBuilding() + dataBean.getUnit_name() + dataBean.getFloor() + dataBean.getHose_number());
                    beanList.remove(dataBean);
                } else {
                    tvCurrHouse.setVisibility(View.GONE);
                    llCurrHouse.setVisibility(View.GONE);
                    llNoData.setVisibility(View.VISIBLE);
                    tvOtherHouse.setVisibility(View.GONE);
                }

                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
